package com.cole.gx095.controller;


import com.cole.gx095.Repository.ArgumentRepository;
import com.cole.gx095.Repository.SalebillRepository;
import com.cole.gx095.controller.utils.CommonRep;
import com.cole.gx095.controller.utils.RedisUtil;
import com.cole.gx095.entity.Argument;
import com.cole.gx095.entity.Salebill;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dict")
@CrossOrigin
@Transactional
public class dictController {
    @Autowired
    ArgumentRepository argumentRepository;
    @Autowired
    SalebillRepository salebillRepository;

    @GetMapping("/List")
    CommonRep List(Integer currentPage, Integer pageSize){
        Page<Argument> page1 = argumentRepository.findAll(PageRequest.of(currentPage - 1, pageSize));
        page1.getContent().forEach(System.out::println);
        return CommonRep.ok()
                .data("page",page1);
    }
    @PostMapping("/modify")
    CommonRep modify(@RequestBody Argument argument){
        System.out.println("argument = " + argument.getValue());
        if(argument.getValue() <= 0 || argument.getValue() > 100.0 ) {
            return CommonRep.warn("请输入正确的值");
        }
        else if(Double.doubleToLongBits(argument.getValue())==Double.doubleToLongBits(argumentRepository.getById(argument.getId()).getValue())){
            return CommonRep.warn("值没有改变");
        }
        else {
            argument.setLastModifyName(RedisUtil.mget("LoginName"));
            argumentRepository.save(argument);
            //修改发票状态
            List<Salebill> all = salebillRepository.findAll();
            for (Salebill salebill : all) {
                if(salebill.getCorrelationRate() > argument.getValue())
                    salebill.setStatus(1);
                else
                    salebill.setStatus(0);
                salebillRepository.save(salebill);
            }
        }
        return CommonRep.ok();
    }
}
